<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Address extends Model
{
    protected $fillable = [
    	'full_name',
    	'phone',
    	'street',
    	'province',
    	'city',
    	'district',
    	'post_code',
    	'is_default',
    	'user_id',
    ];

    public function user()
    {
    	return $this->belongsTo(User::class, 'user_id');
    }

    public function getFullAddressAttribute($v)
    {
    	return $this->province.$this->city.$this->district.$this->street.'。   '.$this->phone.' '.$this->full_name;
    }

    public static function boot()
    {
        parent::boot();
        static::saving(function($address) {
            if($address->is_default)
            {
                $address->user->addresses()->where('is_default', 1)->update(['is_default' => 0]);
            }
        });
    }
}
